package com.xd.common.util;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Map;

@Component
public class RestTemplateUtil {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * GET 请求
     *  "http://localhost:8080/api/user/{id}";
     */
    public <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) {
        return restTemplate.getForEntity(url, responseType, uriVariables);
    }

    /**
     * GET 请求
     * http://localhost:8080/api/user?name=tom&age=20
     */

    public <T> ResponseEntity<T> getWithParams(String baseUrl, Class<T> responseType, Map<String, ?> queryParams) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
        if (queryParams != null) {
            queryParams.forEach((key, value) -> builder.queryParam(key, value));
        }
        URI uri = builder.build().encode().toUri();
        return restTemplate.getForEntity(uri, responseType);
    }

    /**
     * POST 请求（发送 JSON）
     */
    public <T> ResponseEntity<T> postJson(String url, Object requestBody, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<Object> request = new HttpEntity<>(requestBody, headers);
        return restTemplate.postForEntity(url, request, responseType);
    }
    /**
     * POST 请求（表单）
     */
    public <T> ResponseEntity<T> postForm(String url, Map<String, Object> formParams, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 表单格式

        // 构造表单参数体
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.setAll(formParams); // 把 Map<String, String> 转成 MultiValueMap

        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);

        return restTemplate.postForEntity(url, request, responseType);
    }

    /**
     * PUT 请求（发送 JSON）
     */
    public <T> ResponseEntity<T> putJson(String url, Object requestBody, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<Object> request = new HttpEntity<>(requestBody, headers);
        return restTemplate.exchange(url, HttpMethod.PUT, request, responseType);
    }

    /**
     * DELETE 请求
     */
    public <T> ResponseEntity<T> delete(String url, Class<T> responseType, Map<String, ?> uriVariables) {
        return restTemplate.exchange(url, HttpMethod.DELETE, null, responseType, uriVariables);
    }

    public static void main(String[] args) throws Exception {
        String s = AESUtil.generateAESKey(128);
        System.out.println(s);
    }
}
